﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Media.Effects;
using System.Threading;
using WGMClient.HelperClasses;
using System.IO;


namespace WGMClient.Controls
{
    /// <summary>
    /// Interaction logic for BackgroundTextBlock.xaml
    /// </summary>
    public partial class BackgroundTextBlock : UserControl
    {
        private WGMLocalClient _associatedClient;
        public WGMLocalClient AssociatedClient { get { return _associatedClient; } set { _associatedClient = value; _associatedClient.Tag = this; } }
        public string ID { get; set; }
        private bool _imageOnLeft;
        private bool _underlineFlashing = false;
        public byte[] ImageData { get; set; }
        public object Tag { get; set; }
        private TabItem _associatedTab;
        public TabItem AssociatedTab 
        { 
            get{ return _associatedTab;}
            set { _associatedTab = value; _associatedTab.Tag = this; }
        }
        public BackgroundTextBlock()
        {
            InitializeComponent();


            
        }


        public bool IsSelected
        {
            get { return AssociatedTab != null && AssociatedTab.IsSelected; }
            set
            {
                if (AssociatedTab != null)
                {
                    AssociatedTab.IsSelected = value;
                    UnderlineVisible = value;

                }
                if (AssociatedClient != null)
                {
                    if (!AssociatedClient.FirstImageCheckCompleted && value == true && !AssociatedClient.IsMainWindow)
                    {
                        HelperClasses.Registry.Engine.SendMessageToServer(new WGMClient.WGMSvc.WGMMessage() { MessageType = WGMClient.WGMSvc.MessageTypes.USER_REQUEST_PROFILE_IMAGE, SenderLogon = HelperClasses.Registry.Username, ReceiverLogon = AssociatedClient.Username, SenderTimeStamp = DateTime.Now });
                        AssociatedClient.FirstImageCheckCompleted = true;
                    }
                }
                if (value == true)
                {                    
                    _underlineFlashing = false;
                    HelperClasses.Registry.MainWindow.ClearDropDownMessage(ID);
                }
            }
        }
        public double MaxTextWidth
        {
            get { return _label.MaxWidth; }
            set { _label.MaxWidth = _label2.MaxWidth = value; }
        }
        public double UnderlineWidth
        {
            get { return _underline.Width; }
            set { _underline.Width = value; }
        }
        public bool BlurImage
        {
            set { _imageBorder.Effect = new BlurEffect(); }
        }
        private Color _imageGlowColor;
        public Color ImageGlowColor
        {
            get { return _imageGlowColor; }
            set { _imageGlowColor = value; SetGlowImage(_glowImage); }
        }
        private double _imageGlowRadius;
        public double ImageGlowRadius 
        {
            get { return _imageGlowRadius; }
            set { _imageGlowRadius = value; SetGlowImage(_glowImage); }
        }
        private bool _glowImage;
        public bool GlowImage
        {
            get { return _glowImage; }
            set 
            {
                _glowImage = value;
                SetGlowImage(value);
            }
        }
        public void SetGlowImage(bool value)
        {
            this.Dispatcher.Invoke
            (
                new Action
                (
                    delegate
                    {
                        if (value)
                            _imageBorder.Effect = new DropShadowEffect() { Color = ImageGlowColor, BlurRadius = ImageGlowRadius, ShadowDepth = 5 };
                        else
                            _imageBorder.Effect = null;
                    }
                )
            );
        }
        public void SetBackgroundImage(byte[] imageData)
        {
            if (imageData != null)
            {
                BackgroundImage = HelperClasses.HelperFunctions.GetBitmapFromImage(HelperClasses.HelperFunctions.ByteArrayToImage(imageData));
                ImageData = imageData;
                ContextMenu menu = new ContextMenu();
                MenuItem mi = new MenuItem();
                mi.Header = "Save Image";
                mi.Click+=new RoutedEventHandler(SaveImageClick);
                menu.Items.Add(mi);
                _imageBorder.ContextMenu = menu;

                Image img = new Image();
                img.Source = HelperClasses.HelperFunctions.GetBitmapFromImage(HelperClasses.HelperFunctions.ByteArrayToImage(imageData));
                img.Width = 500;
                img.Height = 500;
                img.Stretch = Stretch.Uniform;
                SetImageTooltip(img);

            }
            else
            {
                _imageBorder.ContextMenu = null;
                System.Drawing.Image img = System.Drawing.Image.FromFile(System.Windows.Forms.Application.StartupPath + "\\Images\\blank2.png");
                BackgroundImage = HelperClasses.HelperFunctions.GetBitmapFromImage(img);
                ImageData = null;
                SetImageTooltip(null);
            }
        }

        void SaveImageClick(object sender, RoutedEventArgs e)
        {
            try
            {
                if (ImageData != null)
                {
                    System.Windows.Forms.SaveFileDialog sfd = new System.Windows.Forms.SaveFileDialog();
                    if (sfd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                    {
                        MemoryStream ms = new MemoryStream(ImageData);
                        System.Drawing.Image newImage = System.Drawing.Image.FromStream(ms);
                        newImage.Save(sfd.FileName);
                        ms.Close();
                    }
                }
            }
            catch { }
        }
        public ImageSource BackgroundImage
        {
            get { return (_imageBorder.Background as ImageBrush).ImageSource; }
            set { _imageBorder.Background = new ImageBrush(value); }
        }
        public bool HasImage
        {
            get { return _imageBorder.Visibility == Visibility.Visible; }
            set
            {
                if (value)
                    _imageBorder.Visibility = Visibility.Visible;
                else
                    _imageBorder.Visibility = Visibility.Collapsed;
            }
        }
        public bool ImageOnLeft
        {
            get { return _imageOnLeft; }
            set
            {
                _imageOnLeft = value;
                if (value == true)
                {
                    Grid.SetColumn(_imageBorder, 0);
                    Grid.SetColumn(_label,1 );
                    Grid.SetColumn(_label2, 1);
                    Grid.SetColumn(_underline, 1);

                }
            }
        }
        public HorizontalAlignment UnderlineAlignment
        {
            get { return _underline.HorizontalAlignment; }
            set { _underline.HorizontalAlignment = value; }
        }
        public Brush UnderlineColour
        {
            get { return _underline.Fill; }
            set { _underline.Fill = value; }
        }
        public bool UnderlineVisible
        {
            get { return _underline.Visibility == Visibility.Visible; }
            set
            {
                if (value)
                    _underline.Visibility = Visibility.Visible;
                else
                    _underline.Visibility = Visibility.Collapsed;
            }
        }
        public void SetTextTooltip(object tooltip)
        {
            _label.ToolTip = tooltip;
            _label2.ToolTip = tooltip;
        }
        public void SetImageTooltip(object tooltip)
        {
            _imageBorder.ToolTip = tooltip;
        }
        public CornerRadius ImageCornerRadius
        {
            get { return _imageBorder.CornerRadius; }
            set { _imageBorder.CornerRadius = value; }
        }
        public CornerRadius OuterBorderRadius
        {
            get { return _outerBorder.CornerRadius; }
            set { _outerBorder.CornerRadius = value; }
        }
        public Thickness InnerBorderMargin
        {
            get { return _mainBorder.Margin; }
            set { _mainBorder.Margin = value; }
        }
        public Thickness ImageMargin
        {
            get { return _imageBorder.Margin; }
            set { _imageBorder.Margin = value; }
        }
        public Stretch ImageStretch
        {
            get { return (_imageBorder.Background as ImageBrush).Stretch; }
            set { (_imageBorder.Background as ImageBrush).Stretch = value; }
        }
        public double ImageWidth
        {
            get { return _imageBorder.Width; }
            set { _imageBorder.Width = value; }
        }
        public double ImageHeight
        {
            get { return _imageBorder.Height; }
            set { _imageBorder.Height = value; }
        }
        public Brush OuterBorderBackGround
        {
            get { return _outerBorder.Background; }
            set { _outerBorder.Background = value; }
        }
        public Brush BorderBackground
        {
            get { return _mainBorder.Background; }
            set { _mainBorder.Background = value; }
        }
        public CornerRadius BorderRadius
        {
            get { return _mainBorder.CornerRadius; }
            set { _mainBorder.CornerRadius = value; }
        }
        public string Text
        {
            get { return _label.Text; }
            set { _label.Text = value; }
        }
        public string SubText
        {
            get { return _label2.Text; }
            set 
            { 
                _label2.Text = value;
                if (_label2.Text.Trim() != "")
                {
                    _label2.Visibility = Visibility.Visible;
                }
                else
                {
                    _label2.Visibility = Visibility.Collapsed;
                }
            }
        }
        public Brush LabelForeground
        {
            get { return _label.Foreground; }
            set { _label.Foreground = value; }
        }
        public Brush SubLabelForeground
        {
            get { return _label2.Foreground; }
            set { _label2.Foreground = value; }
        }
        public FontFamily LabelFontFamily
        {
            get { return _label.FontFamily; }
            set { _label.FontFamily = value; }
        }
        public FontFamily SubLabelFontFamily
        {
            get { return _label2.FontFamily; }
            set { _label2.FontFamily = value; }
        }
        public FontWeight LabelFontWeight
        {
            get { return _label.FontWeight; }
            set { _label.FontWeight = value; }
        }
        public FontWeight SubLabelFontWeight
        {
            get { return _label2.FontWeight; }
            set { _label2.FontWeight = value; }
        }
        public FontStyle LabelFontStyle
        {
            get { return _label.FontStyle; }
            set { _label.FontStyle = value; }
        }
        public HorizontalAlignment LabelAlignment
        {
            get { return _label.HorizontalAlignment; }
            set { _label.HorizontalAlignment = value; }
        }
        public HorizontalAlignment SubLabelAlignment
        {
            get { return _label2.HorizontalAlignment; }
            set { _label2.HorizontalAlignment = value; }
        }
        public FontStyle SubLabelFontStyle
        {
            get { return _label2.FontStyle; }
            set { _label2.FontStyle = value; }
        }
        public double LabelFontSize
        {
            get { return _label.FontSize; }
            set { _label.FontSize = value; }
        }
        public double SubLabelFontSize
        {
            get { return _label2.FontSize; }
            set { _label2.FontSize = value; }
        }
        public Thickness TextMargin
        {
            get { return _label.Margin; }
            set { _label.Margin = value; }
        }
        public Thickness SubTextMargin
        {
            get { return _label2.Margin; }
            set { _label2.Margin = value; }
        }
        public void FlashUnderline()
        {
            ThreadPool.QueueUserWorkItem
            (
                delegate
                {
                   
                    if (_underlineFlashing)
                        return;
                    _underlineFlashing = true;

                    SetUnderlineVisible(true);
                    while (_underlineFlashing)
                    {
                        if (_underlineFlashing)
                            SetUnderlineColour(Colors.DarkRed);
                        Thread.Sleep(500);
                        if(_underlineFlashing)
                            SetUnderlineColour(Colors.Transparent);
                        Thread.Sleep(500);
                    }
                    SetUnderlineColour(Colors.OrangeRed);
                    return;
                }
            );
        }
        private void SetUnderlineVisible(bool value)
        {
            this.Dispatcher.Invoke
            (
                new Action
                (
                    delegate
                    {
                        UnderlineVisible = value;
                    }
                )
            );
        }
        private void SetUnderlineColour(Color colour)
        {
            this.Dispatcher.Invoke
            (
                new Action
                (
                    delegate
                    {
                        UnderlineColour = new SolidColorBrush(colour);
                    }
                )
            );
        }

        private void OnImageRightClick(object sender, MouseButtonEventArgs e)
        {

        }

    }
}
